//
//  ERNetworkTools.m
//  EnjoyRunning
//
//  Created by ljm on 2017/3/16.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "ERNetworkTools.h"

@implementation ERNetworkTools

#pragma mark -- 实现单例类
+ (instancetype)shareTools
{
    static ERNetworkTools *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        /*
         *    初始化单例类
         */
        instance = [[ERNetworkTools alloc] initWithBaseURL:nil];
        
        /*
         *    增加数据的解析格式
         */
        instance.responseSerializer.acceptableContentTypes = [
                                                              NSSet setWithObjects:@"application/json",@"text/javascript",@"text/json",@"text/html", @"text/plain", nil];
    });
    
    return instance;
}

#pragma mark -- 实现网络请求的方法
- (void)requestWithMethod:(requestMode)method andUrl:(NSString *)url andParameters:(NSDictionary *)parameters andCallBack:(networkBlock)finshed
{
    if (method == POST) {
        
        /*
         *    POST请求
         */
        [self POST:url
        parameters:parameters
          progress:nil
           success:
         ^(NSURLSessionDataTask * _Nonnull task, id
           _Nullable responseObject) {
            //成功
            finshed(responseObject,nil);
               
        }
           failure:
         ^(NSURLSessionDataTask * _Nullable task,
           NSError * _Nonnull error) {
            //失败
            finshed(nil,error);
        }];
    }else
        
    {
    /*
     *    GET请求
     */
        [self GET:url
       parameters:parameters
         progress:nil
          success:
         ^(NSURLSessionDataTask * _Nonnull task,
           id  _Nullable responseObject) {
             
            finshed(responseObject,nil);
             
        }
          failure:
         ^(NSURLSessionDataTask * _Nullable task,
           NSError * _Nonnull error) {
            finshed(nil,error);
        }];
        
    }
}

@end
